package com.wondertek.mist.photo;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ExifInterface;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Environment;

/**
 * 文件路径配置、图片压缩函数封装在此类中
 * 
 * @author Administrator
 * 
 */
public class AppConfig
{
	public static String DIR = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "cfub" + File.separator;
	public static String TEMP_DIR = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "cfub" + File.separator + "temp" + File.separator;
	public static String ZIP_DIR = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "cfub" + File.separator + "zip" + File.separator;

	/**
	 * * 读取图片属性：旋转的角度 *
	 * 
	 * @param path
	 *            图片绝对路径 *
	 * @return degree旋转的角度
	 */
	public static int readPictureDegree(String path)
	{
		int degree = 0;
		try
		{
			ExifInterface exifInterface = new ExifInterface(path);
			int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
			switch (orientation)
			{
			case ExifInterface.ORIENTATION_ROTATE_90:
				degree = 90;
				break;
			case ExifInterface.ORIENTATION_ROTATE_180:
				degree = 180;
				break;
			case ExifInterface.ORIENTATION_ROTATE_270:
				degree = 270;
				break;
			}
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		return degree;
	}

	/**
	 * 旋转图片
	 */
	public static Bitmap rotaingImageView(int angle, Bitmap bitmap)
	{
		// 旋转图片 动作
		Matrix matrix = new Matrix();
		matrix.postRotate(angle);
		// 创建新的图片
		Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
		return resizedBitmap;
	}

	/**
	 * 压缩图片到本地
	 * 
	 * @param bmp
	 * @param file
	 * @return
	 */
	public static void compressBmpToFile(Bitmap bmp, File file)
	{
		// String photoCode = "";
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		int options = 80;
		bmp.compress(Bitmap.CompressFormat.PNG, options, baos);
		try
		{
			FileOutputStream fos = new FileOutputStream(file);
			fos.write(baos.toByteArray());
			fos.flush();
			fos.close();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		// return photoCode;
	}

	// 压缩图片到内存
	public static Bitmap compressImageFromFile(String srcPath, Context context)
	{
		BitmapFactory.Options newOpts = new BitmapFactory.Options();
		newOpts.inJustDecodeBounds = true; // 只读边,不读内容
		Bitmap bitmap = BitmapFactory.decodeFile(srcPath, newOpts);
		newOpts.inJustDecodeBounds = false;
		int w = newOpts.outWidth;
		int h = newOpts.outHeight;
		// float ww = 100 *
		// (int)context.getResources().getDisplayMetrics().density;
		float ww = 240;
		float hh = ww;
		int be = 1;
		if (w > h && w > ww)
		{
			be = (int) (newOpts.outWidth / ww);
		}
		else if (w < h && h > hh)
		{
			be = (int) (newOpts.outHeight / hh);
		}
		if (be <= 0)
		{
			be = 1;
		}
		newOpts.inSampleSize = be; // 设置采样率

		newOpts.inPreferredConfig = Config.ARGB_8888; // 该模式是默认的,可不设
		newOpts.inPurgeable = true; // 同时设置才会有效
		newOpts.inInputShareable = true; // 当系统内存不够时候图片自动被回收
		bitmap = BitmapFactory.decodeFile(srcPath, newOpts);
		return bitmap;
	}

	/***
	 * 图片的缩放方法
	 * 
	 * @param bgimage
	 *            ：源图片资源
	 * @param newWidth
	 *            ：缩放后宽度
	 * @param newHeight
	 *            ：缩放后高度
	 * @return
	 */
	public static Bitmap zoomImage(Bitmap bgimage, double newWidth, double newHeight)
	{
		// 获取这个图片的宽和高
		float width = bgimage.getWidth();
		float height = bgimage.getHeight();
		// 创建操作图片用的matrix对象
		Matrix matrix = new Matrix();
		// 计算宽高缩放率
		float scaleWidth = ((float) newWidth) / width;
		float scaleHeight = ((float) newHeight) / height;
		// 缩放图片动作
		matrix.postScale(scaleWidth, scaleHeight);
		Bitmap bitmap = Bitmap.createBitmap(bgimage, 0, 0, (int) width, (int) height, matrix, true);
		return bitmap;
	}

	/** 查看网络是否可用 */
	public static boolean isConnect(Context context)
	{
		ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		if (connectivity != null)
		{
			NetworkInfo[] info = connectivity.getAllNetworkInfo();
			if (info != null)
			{
				for (int i = 0; i < info.length; i++)
				{
					if (info[i].getState() == NetworkInfo.State.CONNECTED)
					{
						return true;
					}
				}
			}
		}
		return false;
	}

	/**
	 * 压缩图片，输出到原来位置
	 * 
	 * @param image
	 * @return
	 */
	public static void compressImage(File path)
	{
		try
		{
			Bitmap image = BitmapFactory.decodeFile(path.getAbsolutePath());
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			image.compress(Bitmap.CompressFormat.JPEG, 80, baos);// 质量压缩方法，这里100表示不压缩，把压缩后的数据存放到baos中
			FileOutputStream fos = new FileOutputStream(path);
			fos.write(baos.toByteArray());
			fos.flush();
			fos.close();
		}
		catch (Exception e)
		{
			e.printStackTrace();
			System.out.println(e.toString());
		}
	}
}